package com.hu.ta.assignment5.io;

import java.io.File;
import java.io.FilenameFilter;

public class FileListFilter implements FilenameFilter {
	private final String name;
	private final String extension;
	private final int	maxFiles;
	private int numFiles = 0;

	public FileListFilter(String name, String extension, int maxFiles) {
		this.name = name;
		this.extension = extension;
		this.maxFiles = maxFiles;
	}

	public boolean accept(File directory, String filename) {
		boolean fileOK = true;

		if (name != null) {
			fileOK &= filename.startsWith(name);
		}

		if (extension != null) {
			fileOK &= filename.endsWith('.' + extension);
		}
		
		if (fileOK) {
			numFiles++;
			if (numFiles>maxFiles) {
				fileOK = false;
			}
		}
		
		return fileOK;
	}
}
